package app.recycler.com.recycler.contacts;

import android.support.annotation.Keep;
import android.support.annotation.NonNull;

@Keep
public class UploadContactBean implements Comparable<UploadContactBean> {
    public String name;
    public String phoneNumber;
    public String avatarUrl;
    public String pinyin; // 姓名对应的拼音
    public String firstLetter; // 拼音的首字母
    public boolean isContainsEmoji;

    public boolean isIncise;

    public UploadContactBean() {
    }

    public UploadContactBean(String name, String phoneNumber) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        char codePoint = name.charAt(0);
        if (!isEmojiCharacter(codePoint)) { // 如果不能匹配,则该字符是Emoji表情
            isContainsEmoji = true;
        } else {
            isContainsEmoji = false;
        }
        if (isContainsEmoji) {
            pinyin = "A11111111" + Cn2Spell.getPinYin(name); // 根据姓名获取拼音
            firstLetter = "#";
        } else {
            pinyin = Cn2Spell.getPinYin(name); // 根据姓名获取拼音
            firstLetter = pinyin.substring(0, 1).toUpperCase(); // 获取拼音首字母并转成大写
            if (!firstLetter.matches("[A-Z]")) { // 如果不在A-Z中则默认为“#”
                firstLetter = "#";
            }
        }
    }

    /**
     * 判断是否是Emoji
     *
     * @param codePoint 比较的单个字符
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    @Override
    public int compareTo(@NonNull UploadContactBean another) {
        if (firstLetter.equals("#") && !another.firstLetter.equals("#")) {
            return 1;
        } else if (!firstLetter.equals("#") && another.firstLetter.equals("#")) {
            return -1;
        } else {
            return pinyin.compareToIgnoreCase(another.pinyin);
        }
    }

    @Override
    public String toString() {
        return "UploadContactBean{" +
                "name='" + name + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", avatarUrl='" + avatarUrl + '\'' +
                ", pinyin='" + pinyin + '\'' +
                ", firstLetter='" + firstLetter + '\'' +
                ", isContainsEmoji=" + isContainsEmoji +
                '}';
    }
}